﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace SharpDiary.Core.Controls
{
    [DefaultEvent("MouseClick")]
    public partial class ButtonControl : UserControl
    {
        private MouseStatus _status = MouseStatus.Normal;

        private Image _imageNormal;

        public Image ImageNormal
        {
            get { return _imageNormal; }
            set
            {

                _imageNormal = value;

                UpdateRegion();
                Invalidate();
            }
        }

        private Image _imageOnMouseOver;

        public Image ImageOnMouseOver
        {
            get { return _imageOnMouseOver; }
            set { _imageOnMouseOver = value; }
        }

        private Image _imageOnPressed;

        public Image ImageOnPressed
        {
            get { return _imageOnPressed; }
            set { _imageOnPressed = value; }
        }

        private Image _imageOnDisabled;

        public Image ImageOnDisabled
        {
            get { return _imageOnDisabled; }
            set { _imageOnDisabled = value; }
        }

        public ButtonControl()
        {
            InitializeComponent();

            SetStyle(ControlStyles.AllPaintingInWmPaint, true);

            ResizeRedraw = true;
            DoubleBuffered = true;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            Image image = _imageNormal;

            switch (_status)
            {
                case MouseStatus.Normal:
                    {
                        image = _imageNormal;
                        break;
                    }
                case MouseStatus.MouseEnter:
                    {
                        image = _imageOnMouseOver;
                        break;
                    }
                case MouseStatus.MouseClick:
                    {
                        image = _imageOnPressed;
                        break;
                    }
            }

            if (image != null)
            {
                e.Graphics.DrawImage(image, e.ClipRectangle);
            }
        }

        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);

            UpdateRegion();
        }

        protected override void OnMouseEnter(EventArgs e)
        {
            base.OnMouseEnter(e);
            _status = MouseStatus.MouseEnter;

            Invalidate();
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            _status = MouseStatus.Normal;

            Invalidate();
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);

            if (e.Button == MouseButtons.Right) { return; }
            _status = MouseStatus.MouseClick;

            Invalidate();
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);

            if (e.Button == MouseButtons.Right) { return; }
            _status = MouseStatus.MouseEnter;

            Invalidate();
        }

        private void UpdateRegion()
        {
            if (_imageNormal == null) { return; }

            GraphicsPath gp = BitmapRegion.CalculateControlGraphicsPath(_imageNormal as Bitmap);
            this.Region = new Region(gp);
        }
    }

    internal enum MouseStatus
    {
        Normal,

        MouseEnter,

        MouseClick
    }
}
